package com.gitee.loyo.spring

import com.gitee.loyo.act.TUserModel
import com.gitee.loyo.act.User
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Propagation
import org.springframework.transaction.annotation.Transactional


@Service
open class TestTransactionService{
    val log = LoggerFactory.getLogger(TestTransactionService::class.java)

    @Transactional
    open fun failToCreateUsers(){
        TUserModel.add(User(userId = 1L, userName = "admin", sex = 1, active = false))
        TUserModel.add(User(userId = 2L, userName = "guest", sex = 1, active = false))
        throw RuntimeException()
    }

    @Transactional(propagation = Propagation.NEVER)
    open fun neverTranCreateUsers(){
        TUserModel.add(User(userId = 1L, userName = "admin", sex = 1, active = false))
        TUserModel.add(User(userId = 2L, userName = "guest", sex = 1, active = false))
    }

    @Transactional(propagation = Propagation.NESTED)
    open fun nestTranCreateUsers(){
        TUserModel.add(User(userId = 1L, userName = "admin", sex = 1, active = false))
        TUserModel.add(User(userId = 2L, userName = "guest", sex = 1, active = false))
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    open fun newTranCreateUsers(){
        TUserModel.add(User(userId = 1L, userName = "admin", sex = 1, active = false))
        TUserModel.add(User(userId = 2L, userName = "guest", sex = 1, active = false))
    }
}